package resoft.jeasyform.components;

import charva.awt.event.ItemListener;
import charva.awt.event.ItemEvent;

import java.util.List;
import java.util.ArrayList;

import charvax.swing.JComboBox;
import charvax.swing.DefaultComboBoxModel;
import resoft.jeasyform.event.InputFieldEventAware;
import resoft.jeasyform.event.EventHandler;
import resoft.jeasyform.event.DefaultEventHandler;
import resoft.jeasyform.event.EventType;

/**
 * <p>select list</p>
 * User: albert
 * Date: Aug 27, 2007
 * Time: 11:52:25 AM
 */
public class SelectionField extends Component implements InputFieldEventAware, AcceptFocusAware {
    public SelectionField() {
        setAcceptFocus(true);
    }

    /**
     * set list map
     *
     * @param options String format:    1:Albert;2:dbo
     */
    public void setOptions(String options) {
        textList.clear();
        valueList.clear();
        String[] pairs = options.split(";");
        for (int i = 0; i < pairs.length; i++) {
            String[] keyValue = pairs[i].split(":");
            String text = keyValue[1];
            String value = keyValue[0];
            textList.add(text);
            valueList.add(value);
        }
        if (jComboBox != null) {
            jComboBox.removeAllItems();
            DefaultComboBoxModel model = new DefaultComboBoxModel(textList.toArray());
            jComboBox.setModel(model);
        }
    }


    public void setMaxRowCount(int maxRowCount) {
        this.maxRowCount = maxRowCount;
    }

    /**
     * getText
     *
     * @return String
     */
    public String getText() {
        if (jComboBox != null) {
            return (String) jComboBox.getSelectedItem();
        } else {
            return null;
        }
    }

    /**
     * getValue
     *
     * @return String
     */
    public String getvalue() {
        String text = getText();
        if (text != null) {
            int index = textList.indexOf(text);
            if (index >= 0) {
                return (String) valueList.get(index);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * to swing object
     *
     * @return charva.awt.Component
     */
    public charva.awt.Component getSwingObject() {

        jComboBox = new JComboBox(textList.toArray());
        if (maxRowCount > 0) {
            jComboBox.setMaximumRowCount(maxRowCount);
        }
        jComboBox.setBounds(getY(), getX(), getY() + getHeight(), getX() + getWidth());
        jComboBox.setPaddingChar('_');
        jComboBox.setColumns(getWidth());

        jComboBox.addItemListener(new DefaultItemListener(getParentWindow(), this, jComboBox, postHandler));

        return jComboBox;
    }

    private class DefaultItemListener implements ItemListener {
        DefaultItemListener(Window parentWindow, SelectionField field, JComboBox jComboBox, EventHandler postHandler) {
            this.parentWindow = parentWindow;
            this.field = field;
            this.jComboBox = jComboBox;
            this.postHandler = postHandler;
        }

        public void itemStateChanged(ItemEvent e) {
            jComboBox.setEnabled(false);
            if (postHandler.execute(parentWindow, field, EventType.TextFieldPostEvent)) {
                parentWindow.changeFocus(field);
            }
        }

        private Window parentWindow;
        private SelectionField field;
        private JComboBox jComboBox;
        private EventHandler postHandler;
    }

    /**
     * hide
     */
    public void hide() {
        jComboBox.setVisible(false);
        setAcceptFocus(false);
    }

    /**
     * show
     */
    public void show() {
        jComboBox.setVisible(true);
        setAcceptFocus(true);
    }


    /**
     * get focus
     */
    public void requestFocus() {
        if (jComboBox != null) {
            jComboBox.requestFocus();
        }
        preHandler.execute(getParentWindow(), this, EventType.TextFieldPostEvent);
    }

    /**
     * register pre event
     *
     * @param preHandler EventHandler
     */
    public void registerPreEvent(EventHandler preHandler) {

    }

    /**
     * register post event
     *
     * @param postHandler EventHandler
     */
    public void registerPostEvent(EventHandler postHandler) {

    }


    private JComboBox jComboBox = null;

    private List textList = new ArrayList();
    private List valueList = new ArrayList();

    private int maxRowCount = 0;

    private EventHandler preHandler = new DefaultEventHandler();
    private EventHandler postHandler = new DefaultEventHandler();


}


